<?php
/*
 * 来路分析相关
 * copyright:living10
 */
class OriginAnalysis_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('session');
 	}
	//来路分析
	public function showrefer($sdate,$edate)
	{
		$int_sdate = strtotime($sdate);
		$int_edate = strtotime($sdate." +1 day");
		$where = " where wid=".$this->session->userdata('wid')." and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate;
		$sql = "SELECT refer,count(arriveCount) as arriveCount,vid,ip,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew, " .
				" sum(stopTime) as stopTimes from tj_stat  " . $where.
				"  group by refer,vid";
		$query = $this->db->query($sql);
		$arr = $vid_tmp = $result = array();
		$str='';
		//有值的用1代替
		foreach($query->result_array()  as $key=>$val){
			$arr[$val['refer']]['stopTimes'][] = $val['stopTimes'];
			$arr[$val['refer']]['vids'][] = 1;
			$arr[$val['refer']]['arriveCount'][] = $val['arriveCount'];
			$arr[$val['refer']]['isnew'][] = $val['isnew'];
			$arr[$val['refer']]['ip'][] = 1;
		}
		foreach($arr as $k=>$v)
		{
			$result[$k]['stopTimes'] = array_sum($v['stopTimes']);
			$result[$k]['vids'] = array_sum($v['vids']);
			$result[$k]['ip'] = array_sum($v['ip']);
			$result[$k]['arriveCount'] = array_sum($v['arriveCount']);
			$result[$k]['isnew'] = array_sum($v['isnew']);
		}

		return $result;
	}

		//来路域名分析
	public function showDomain($sday,$eday)
	{
		$int_sdate = strtotime($sday);

		$int_edate = strtotime($eday." +1 day");
		$where = " and (keyword!='inner' and keyword!='地址栏进入' and refer_domain!='')";
		$sql = "SELECT sum(stopTime) as stopTimes,max(arriveCount) as total_arrive,searchEngine,sum(isbad)".
				" as badclicks,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,refer_domain,ip,vid from tj_stat where  ".
				" wid=".$this->session->userdata('wid')." and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate.$where.
				" GROUP BY refer_domain,vid,ip";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();

		foreach($arr as $kk=>$vv)
		{
			$key = $vv['refer_domain'];
			@$data[$key]['tmp_searchEngine'][]= $vv['searchEngine'];
			@$data[$key]['tmp_vids'][]= $vv['vid'];
			@$data[$key]['tmp_ips'][]= $vv['ip'];
			@$data[$key]['total_stopTimes'] +=$vv['stopTimes'];
			@$data[$key]['total_arrives'] +=$vv['total_arrive'];
			@$data[$key]['total_isnew'] +=$vv['isnew'];
		}
		if($data)
		{

			foreach($data as $k=>$v)
			{
				$data[$k]['total_searchEngine'] = count(array_unique($v['tmp_searchEngine']));
				$data[$k]['avg_arriveTimes'] = round($v['total_stopTimes']/$v['total_arrives'],2);
				$data[$k]['total_ips'] = count(array_unique($v['tmp_ips']));
				$data[$k]['total_vids'] = count(array_unique($v['tmp_vids']));
				$data[$k]['avg_searchTimes'] = round($data[$k]['total_searchEngine']/$data[$k]['total_vids'],2)*100;
				unset($data[$k]['tmp_searchEngine']);
				unset($data[$k]['tmp_ips']);
				unset($data[$k]['tmp_vids']);
			}
		}
		$caption_name = "关键词统计图";
		$xml = $this->make_xml($data,$caption_name,'total_searchEngine');
		$result_arr = array();
		$result_arr['data'] = $data;
		$result_arr['xml'] = $xml;

		return $result_arr;
	}
	 	function make_xml($data,$caption_name,$value_name)
 	{


		$str = '<?xml version="1.0" encoding="gb2312"?>';
		$str .= '<chart caption="'.$caption_name.'" baseFontSize="14" palette="2" animation="1" formatNumberScale="0" pieSliceDepth="30" startingAngle="125" showValues="1" showPercentInToolTip="1" showLabels="1" showLegend="1" formatNumber="1">';
		foreach($data as $k=>$v)
		{
			$str .= '<set label="'.$k.'" value="'.$v[$value_name].'" isSliced="1"/>';
		}
		$str .='</chart>';
		return $str;
	}
}
?>
